package com.upay.android.controller.adapter;

import static com.upay.android.controller.helper.Format.format;
import static com.upay.android.controller.helper.Format.formatWeight;

import java.math.BigDecimal;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.upay.android.R;
import com.upay.android.controller.helper.Format;
import com.upay.android.domain.payment.PaymentList;
import com.upay.android.domain.payment.WeightedPaymentDetail;
import com.upay.android.domain.user.UserWrapper;

public class WeightedPaymentArrayAdapter extends ArrayAdapter<WeightedPaymentDetail> {
	
	/* ---- Constants ---- */
	public final static BigDecimal STEP = new BigDecimal(1);
	
	/* ---- Instance Variables ---- */
	private Activity mContext = null;
	private PaymentList<WeightedPaymentDetail> mPaymentList = null;


	/* ---- Constructors ---- */
	
	public WeightedPaymentArrayAdapter(Activity context, PaymentList<WeightedPaymentDetail> paymentList) {
		super(context, R.layout.fragment_row_fixed_payment, paymentList.getPayments());
		this.mContext = context;
		this.mPaymentList = paymentList;
	}
	
	/* ---- ViewHolder ---- */
	
	static class ViewHolder {
		public TextView fullName = null;
		public TextView weightValue = null;
		public TextView groupBalanceValue = null;
		public RadioButton selectionButton = null;
		public ImageView plusButton = null;
		public ImageView minusButton = null;
	}
	
	/* ---- Business Methods ---- */
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflator = mContext.getLayoutInflater();
			rowView = inflator.inflate(R.layout.fragment_row_weighted_payment, null);
			
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.fullName = (TextView)rowView.findViewById(R.id.value_weighted_payment_fullname);
			viewHolder.groupBalanceValue = (TextView)rowView.findViewById(R.id.value_weighted_payment_group_balance);
			viewHolder.weightValue = (TextView)rowView.findViewById(R.id.value_weighted_payment_weight);
			viewHolder.selectionButton = (RadioButton)rowView.findViewById(R.id.button_weighted_payment_selected);
			viewHolder.plusButton = (ImageView)rowView.findViewById(R.id.button_weighted_payment_plus);
			viewHolder.minusButton = (ImageView)rowView.findViewById(R.id.button_weighted_payment_minus);
			
	
			rowView.setTag(viewHolder);
			viewHolder.selectionButton.setTag(mPaymentList.get(position));
			viewHolder.plusButton.setTag(mPaymentList.get(position));
			viewHolder.minusButton.setTag(mPaymentList.get(position));
		} else {
			rowView = convertView;
			((ViewHolder)rowView.getTag()).selectionButton.setTag(mPaymentList.get(position));
			((ViewHolder)rowView.getTag()).plusButton.setTag(mPaymentList.get(position));
			((ViewHolder)rowView.getTag()).minusButton.setTag(mPaymentList.get(position));
		}
		
		WeightedPaymentDetail payment = mPaymentList.get(position);
		ViewHolder viewHolder = (ViewHolder)rowView.getTag();
		viewHolder.selectionButton.setChecked(payment.isSelected());
		viewHolder.fullName.setText(UserWrapper.getFullName(payment.getGroupAccount().getUser()));
		viewHolder.weightValue.setText(formatWeight(payment.getWeightValue()));
		Format.drawBalance(viewHolder.groupBalanceValue, payment.getGroupAccount().getBalance());
		viewHolder.groupBalanceValue.setText(format(payment.getGroupAccount().getBalance()));

		viewHolder.selectionButton.setChecked(mPaymentList.getPayments().get(position).isSelected());
		viewHolder.selectionButton.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View view) {
				mPaymentList.getSelectedPayment().setSelected(Boolean.FALSE);
				RadioButton button = (RadioButton)view; 
				WeightedPaymentDetail weightedPayment = ((WeightedPaymentDetail)button.getTag());				
				weightedPayment.setSelected(Boolean.TRUE);
				button.setSelected(Boolean.TRUE);
				mPaymentList.setSelectedPayment(weightedPayment);
				notifyDataSetChanged();
			}
		});
		viewHolder.plusButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				ImageView button = (ImageView)view;
				View parent = (View)view.getParent();
				TextView weightValue = (TextView)parent.findViewById(R.id.value_weighted_payment_weight);
				WeightedPaymentDetail weightedPayment = ((WeightedPaymentDetail)button.getTag());
				if (10 > weightedPayment.getWeightValue().doubleValue()) {
					weightedPayment.setWeightValue(weightedPayment.getWeightValue().add(STEP));
					weightValue.setText(formatWeight(weightedPayment.getWeightValue()));
				}
			}
		});
		viewHolder.minusButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				ImageView button = (ImageView)view;
				View parent = (View)view.getParent();
				TextView weightValue = (TextView)parent.findViewById(R.id.value_weighted_payment_weight);
				WeightedPaymentDetail weightedPayment = ((WeightedPaymentDetail)button.getTag());
				if (weightedPayment.getWeightValue().doubleValue() >= STEP.doubleValue()) {
					weightedPayment.setWeightValue(weightedPayment.getWeightValue().subtract(STEP));	
					weightValue.setText(formatWeight(weightedPayment.getWeightValue()));
				}
			}
		});			
		
		return rowView;
	}
	
	/* ---- Getters and Setters ---- */

	/**
	 * @return the context
	 */
	public Activity getContext() {
		return mContext;
	}

	/**
	 * @param context the context to set
	 */
	public void setContext(Activity context) {
		this.mContext = context;
	}

}
